package com.start.manager.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.manager.dao.GoodsDao;
import com.start.manager.domain.entity.Goods;
import com.start.manager.domain.vo.GoodsVO;
import com.start.manager.domain.vo.OptionsVO;
import com.start.manager.service.IGoodsService;
import com.start.manager.util.MyUtil;
import com.start.manager.util.PageUtil;
import com.start.manager.util.constant.RedisConstant;
import com.start.manager.util.constant.StatusConstant;
import com.jienlee.common.redis.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lijian
 * @since 2019-12-20
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Goods> implements IGoodsService {

    @Autowired
    private MyUtil myUtil;

    @Override
    public PageUtil findList(GoodsVO goodsVO) {
        QueryWrapper<Goods> queryWrapper = initQueryWrapper(goodsVO);
        if (goodsVO.getSize() == 0) {
            //获取全部的数据
            List<Goods> list = this.baseMapper.selectList(queryWrapper);
            return new PageUtil(list.size(), 0, list);
        }
        //分页查询货物详情
        Page<Goods> page = new Page<>(goodsVO.getPage(), goodsVO.getSize());
        queryWrapper.orderByAsc("id");
        Page<Goods> pageData = this.baseMapper.selectPage(page, queryWrapper);
        return new PageUtil(pageData.getTotal(), pageData.getCurrent(), pageData.getRecords());
    }

    @Override
    public boolean saveGoods(GoodsVO goodsVo) {
        Goods goods = new Goods();
        if (StringUtils.isBlank(goodsVo.getId())) {
            goods.setId(myUtil.generateId("G"));
        }
        BeanUtils.copyProperties(goodsVo, goods);
        int insert = this.baseMapper.insert(goods);
        return insert > 0 ? true : false;
    }

    @Override
    public List<Goods> getOptionsList(OptionsVO optionsVO) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", optionsVO.getType()).eq("status", StatusConstant.USE);
        queryWrapper.orderByAsc("id");
        return this.baseMapper.selectList(queryWrapper);

    }

    private QueryWrapper initQueryWrapper(GoodsVO goodsVO) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper();
        if (StringUtils.isNotBlank(goodsVO.getName())) {
            queryWrapper.like("name", goodsVO.getName());
        }
        if (StringUtils.isNotBlank(goodsVO.getType())) {
            queryWrapper.eq("type", goodsVO.getType());
        }
        if (StringUtils.isNotBlank(goodsVO.getId())) {
            queryWrapper.like("id", goodsVO.getId());
        }
        if (!Objects.isNull(goodsVO.isStatus())) {
            queryWrapper.eq("status", goodsVO.isStatus());
        }
        return queryWrapper;
    }
}
